package com.kitty.game.pet.handler.upgrade;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.attribute.AttrService;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.scene.SceneManager;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.message.MessagePusher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**宠物时装*/
@Component
public class PetFashionHandler extends UpgradePetHandler {

    @Autowired
    AttrService attrService;
    @Override
    public String acceptUpgradePet(Role role, Pet pet, String ids, String itemPos, String costType, String otherPetPos) {
        logger.error("宠物顿悟{},物品id{},物品位置{},{},{}",pet.getName(),ids,itemPos,costType,otherPetPos);
        if(pet.getRank()>0){
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("坐骑无法使用时装！"));
            return null;
        }
        if(costType.equals("0")){
            pet.setSpecial_icon(0);
            SpringUtils.getPetService().refreshPetInfo(role,pet);

            /** 广播**/
            SceneManager.INSTANCE.sendMessages(role, SpringUtils.getBroadcastService().getPetAppear(role));
            return null;
        }

        RoleEquip roleEquip =null;
        for (Map.Entry<Integer, RoleEquip> entry : role.getEquipBox().getEquips().entrySet()) {
            RoleEquip tempEquip = entry.getValue();
            if (tempEquip.getPosition() == Short.parseShort(costType) ) {
                roleEquip = tempEquip;
                break;
            }

        }
        if (roleEquip == null) {
            return null;
        }

        Integer icon, fasionIcon;
        icon = attrService.getValue(roleEquip.getFields().get((short) 1).getField().get((short) 40).getValue());
        fasionIcon = DataCache.ICON_FASION.get(icon);
        pet.setSpecial_icon(fasionIcon);

        SpringUtils.getPetService().refreshPetInfo(role,pet);

        /** 广播**/
        SceneManager.INSTANCE.sendMessages(role, SpringUtils.getBroadcastService().getPetAppear(role));

        return null;
    }
}
